package com.example.joke.custom.textcolor

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.graphics.Color
import android.util.Log
import android.view.ViewGroup
import android.view.animation.DecelerateInterpolator
import android.widget.LinearLayout
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.example.joke.base.navigationbar.AbsNavigationBar
import com.example.joke.databinding.ActivityQqStepBinding
import com.example.joke.databinding.ActivityTextColorBinding
import com.example.joke.frame.BaseSkinActivity
import com.example.joke.frame.DefaultNavigationBar

class TextColorActivity : BaseSkinActivity<ActivityTextColorBinding>() {

    var list: MutableList<String> = arrayListOf("美妆","旅游","健身","学习")
    override fun setListener() {
    }

    override fun initData() {
        val valueAnimator: ValueAnimator = ObjectAnimator.ofFloat(0f, 1f)
        valueAnimator.duration = 5000
        valueAnimator.interpolator = DecelerateInterpolator()
        valueAnimator.addUpdateListener {
            binding.qq0.mCurrentProgress = it.animatedValue as Float
        }
        valueAnimator.start()

        list.forEach {
            val view = ColorTrackTextView(this)
            view.text = it
            view.textSize = 20F
            view.mChangeColor = Color.parseColor("#ff0000")
            val layoutParams = LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)
            layoutParams.weight = 1F
            view.layoutParams = layoutParams
            binding.ll.addView(view)
        }
        val adapter = VPAdapter()
        adapter.list = list
        binding.vp.adapter = adapter
        binding.vp.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                Log.d("OnPageChange---", position.toString().plus("---").plus(positionOffset).plus("---").plus(positionOffsetPixels))
                val left: ColorTrackTextView = binding.ll.getChildAt(position) as ColorTrackTextView
                left.mDirection = ColorTrackTextView.Direction.RIGHT_TO_LEFT
                left.mCurrentProgress = 1 - positionOffset

                try {
                    val right: ColorTrackTextView = binding.ll.getChildAt(position + 1) as ColorTrackTextView
                    right.mDirection = ColorTrackTextView.Direction.LEFT_TO_RIGHT
                    right.mCurrentProgress = positionOffset
                } catch (e: Exception) {

                }
            }
        })
    }

    override fun initTitle() {
        DefaultNavigationBar.Builder(this)
            .setTitle("字体变色")
            .builder<AbsNavigationBar.Builder.AbsNavigationParams>()
    }

    override fun getBinding1(): ActivityTextColorBinding {
        return ActivityTextColorBinding.inflate(layoutInflater)
    }
}